package com.handydata.antifraud.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * 获取当前时间的格式化
     * @param pattern
     * @return
     */
    public static String now(String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(LocalDateTime.now());
    }
    /**
     * 获取当前时间的格式化
     * pattern:yyyyMMdd
     * @return
     */
    public static String now8() {
        return now("yyyyMMdd");
    }
    /**
     * 获取当前时间的格式化
     * pattern:yyyy-MM-dd
     * @return
     */
    public static String now10() {
        return now("yyyy-MM-dd");
    }
    /**
     * 获取当前时间的格式化
     * pattern:yyyyMMddHHmmss
     * @return
     */
    public static String now14() {
        return now("yyyyMMddHHmmss");
    }

    /**
     * 获取当前时间的格式化
     * pattern:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String now19() {
        return now("yyyy-MM-dd HH:mm:ss");
    }
    
    /**
     * 获取当前时间的格式化
     * pattern:yyyyMMddHHmmssSSS
     * @return
     */
    public static String now17() {
        return now("yyyyMMddHHmmssSSS");
    }


    /**
     * 获取当前时间的格式化
     * pattern:yyyy-MM-dd HH:mm:ss:SSS
     * @return
     */
    public static String now18() {
        return now("yyyy-MM-dd HH:mm:ss:SSS");

    }
    



	/**
	 * localDateTime对象转为普通Date对象
	 * @return
	 */
	public static Date localDateTimeToUdate() {
		LocalDateTime localDateTime = LocalDateTime.now();
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return Date.from(instant);
	}

    /**
     * 将字符串格式化为LocalDate
     * @param charSequence
     * @param pattern
     * @return
     */
	public static LocalDate parse(CharSequence charSequence, String pattern) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
        return LocalDate.parse(charSequence, df);
    }


    public static Date getPast () {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.MONTH, -1);
            Date m = c.getTime();
            return format.parse(format.format(m));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }




    /**
     * 将字符串格式化为LocalDate
     * @param charSequence
     * @return
     */
    public static LocalDate parse8(CharSequence charSequence) {
	    return parse(charSequence, "yyyyMMdd");
    }

    public static void main(String[] args) {

        System.out.println(DateUtil.getPast());
    }

}
